// pages/teacher/teacher/teacherhome.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        home: true,
        my: false,
        headimg: '/pages/images/user.png',
        username: '教职工',
        myusedata: [{
                id: 1,
                label: '个人信息',
                img: '/pages/images/teacher/geren.png'
            },
            {
                id: 2,
                label: '活动管理',
                img: '/pages/images/teacher/lishi.png'
            },
            {
                id: 3,
                label: '设置中心',
                img: '/pages/images/shezhi.png'
            },

        ],
        current: 0,
        tabs: ['正在报名','正在进行', '已发布'],
        index: null,
        activity: [],
        right: 10,
        top: 800,
        width: 120,
        height: 120,
        startX: 0,
        startY: 0,
        tdata: [],
        searchValue:''
    },
    homeBar() {
        this.setData({
            home: true,
            my: false
        })
    },
    myBar() {
        this.setData({
            home: false,
            my: true
        })

    },
    tabSelect(e) {
        // console.log(e.currentTarget.dataset.id)
        this.setData({
            current: e.currentTarget.dataset.id
        })
    },
    onTouchStart: function (e) {
        this.setData({
            startX: e.touches[0].clientX,
            startY: e.touches[0].clientY
        })
    },
    onTouchMove: function (e) {
        var offsetX = e.touches[0].clientX - this.data.startX
        var offsetY = e.touches[0].clientY - this.data.startY
        this.setData({
            right: this.data.right - offsetX,
            top: this.data.top + offsetY,
            startX: e.touches[0].clientX,
            startY: e.touches[0].clientY
        })
    },
    onTouchEnd: function (e) {
        this.setData({
            right: 10
        })

    },
    enterActivity(e) {
        var i = e.currentTarget.dataset.id
        var adata = JSON.stringify(this.data.activity[i])
        console.log(adata)
        wx.navigateTo({
            url: '/pages/teacher/teacheractivity/teacheractivity?adata=' + adata,
        })
    },
    searchCoach(e){
        console.log(e.detail.value)
        this.setData({
            searchValue:e.detail.value,
        })
    },
    sendActivity() {
        wx.navigateTo({
            url: '/pages/teacher/teachersend/teachersend',
        })
    },
    
    async LoadData() {
        var sql = 'select * from activity where tno=' + this.data.tdata.user
        if (wx.getStorageSync('avatarUrl')) {
            this.setData({
                headimg: wx.getStorageSync('avatarUrl')
            })
        }
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql,
            },
        })
        console.log(res1.data)
        this.setData({
            activity: res1.data
        })
    },
    
    async getHead(e) {
        var username = '微信用户'
        var headimg = e.detail.avatarUrl
        wx.setStorageSync('nickName', username)
        wx.setStorageSync('avatarUrl', headimg)
        this.setData({
            username: username,
            headimg: headimg
        })

    },
    async mysetBtn(e) {
        console.log(e.currentTarget.dataset.title)
        var title = e.currentTarget.dataset.title

        var url = ''
        var sql = 'select tno,tname,td,tphone from teacher where tno=' + this.data.tdata.user
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql,
            },
        })
        console.log(res1.data[0])
        var signinfo = JSON.stringify(res1.data[0])
        if (title === '设置中心') {
            url = '/pages/teacher/teachersite/teachersite?title=' + title + '&signinfo=' + signinfo
        } else if (title === '个人信息') {
            url = '/pages/teacher/teacheredit/teacheredit?title=' + title + '&signinfo=' + signinfo
        } else {
            url = '/pages/teacher/teachermanage/teachermanage?title=' + title + '&signinfo=' + signinfo
        }
        wx.navigateTo({
            url: url,
        })
    },
    async sqlRequest(sql){
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql,
            },
        })
        return res1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            tdata: wx.getStorageSync('teacher')
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        var username = wx.getStorageSync('nickName')
        var headimg = wx.getStorageSync('avatarUrl')
        if (username) {
            this.setData({
                username: username,
                headimg: headimg
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.LoadData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})